package com.jz.community.basecomm.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.mvp.MVPContract;
import com.jz.community.basecomm.net.retrofit.ApiException;
import com.jz.community.basecomm.routerUtils.RouterCommonUtils;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.utils.LoggerUtils;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.basecomm.utils.eventbus.AppEvent;
import com.jz.community.basecommunity.R;
import com.roger.gifloadinglibrary.GifLoadingView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Objects;

import butterknife.ButterKnife;

/**
 * Created by Tony.Fan on 2018/3/15 14:51
 */
public abstract class BaseMvpFragment<V extends MVPContract.View, P extends BaseMvpPresenter<V>> extends SupportFragment {

    protected P mPresenter;

    private ImmersionBar immersionBar;

    protected GifLoadingView gifLoadingView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (getContentViewLayoutID() != 0) {
            View view = inflater.inflate(getContentViewLayoutID(), null);
            ButterKnife.bind(this, view);

            gifLoadingView = new GifLoadingView();
            gifLoadingView.setBackgroundResource(R.drawable.num19);

            initPresenter();
            initView();
            addListener();
            initDatas();
            return view;
        } else {
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    }


    protected void addListener() {

    }

    private void initPresenter() {
        if (mPresenter == null) {
            mPresenter = createPresenter();
        }
    }

    /**
     * 状态栏
     *
     * @param orderHomeToolbar
     */
    protected void setImmersionBar(Toolbar orderHomeToolbar) {
        immersionBar = ImmersionBar.with(this);
        immersionBar
                .titleBar(orderHomeToolbar)
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    /**
     * 状态栏
     *
     * @param orderHomeToolbar
     */
    protected void setImmersionBar(Toolbar orderHomeToolbar, float alpha) {
        immersionBar = ImmersionBar.with(this);
        immersionBar
                .titleBar(orderHomeToolbar)
                .statusBarDarkFont(true, alpha)
                .init();
    }

    /**
     * 状态栏
     *
     * @param orderHomeToolbar
     */
    protected void setImmersionBar(Toolbar orderHomeToolbar, float alpha, boolean fits) {
        immersionBar = ImmersionBar.with(this);
        immersionBar
                .titleBar(orderHomeToolbar)
                .fitsSystemWindows(fits)
                .statusBarDarkFont(true, alpha)
                .init();
    }

    /**
     * 状态栏重叠
     *
     * @param fits
     */
    protected void setImmersionBar(boolean fits) {
        immersionBar = ImmersionBar.with(this);
        immersionBar.statusBarDarkFont(true).fitsSystemWindows(fits).init();
    }

    /**
     * 状态栏颜色
     */
    protected void setImmersionBar(int statusBarColor) {
        immersionBar = ImmersionBar.with(this);
        immersionBar.statusBarColor(statusBarColor).statusBarDarkFont(true).fitsSystemWindows(true).init();
    }

    /**
     * 初始化数据
     */
    protected void initDatas() {

    }

    /**
     * 初始化View
     */
    protected void initView() {

    }

    /**
     * 显示Loading对话框
     */
    protected void showLoading() {
        gifLoadingView.show(Objects.requireNonNull(getActivity()).getFragmentManager(), "");
    }

    /**
     * 关闭Loading对话框
     */
    protected void dismissLoading() {
        gifLoadingView.dismiss();
    }

    protected void showNormal(String msg) {
        WpToast.l(getContext(), msg);
    }

    /**
     * 设置ContentView
     *
     * @return
     */
    protected abstract int getContentViewLayoutID();

    protected abstract P createPresenter();


    //懒加载
    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initLazy(savedInstanceState);
    }

    public View noDataView(RecyclerView recyclerView, String msg) {
        View view = getLayoutInflater().inflate(R.layout.view_empty, (ViewGroup) recyclerView.getParent(), false);
        ImageView empty_iv = view.findViewById(R.id.empty_img);
        TextView empty_tv = view.findViewById(R.id.empty_tv);
        empty_iv.setImageResource(R.mipmap.empty_default);
        empty_tv.setText(msg);
        return view;
    }

    public View noLoginView(RecyclerView recyclerView) {
        View view = getLayoutInflater().inflate(R.layout.view_empty_login, (ViewGroup) recyclerView.getParent(), false);
        ImageView mIvLogin = view.findViewById(R.id.empty_login_img);
        TextView mTvLogin = view.findViewById(R.id.empty_login_btn);
        mIvLogin.setImageResource(R.mipmap.empty_default);
        mTvLogin.setOnClickListener(v -> RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.MODULE_AUTHORIZATION_LOGIN));
        return view;
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        initVisible();
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        initInvisible();
    }


    protected void initVisible() {

    }

    protected void initInvisible() {

    }

    protected void initLazy(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }
}
